<?php
class ControllerNewsArticle extends Controller {
	private $error = array();

	public function index() {
		$this->language->load('news/article');

		$this->load->model('news/article');
		$this->load->model('news/type');
		$this->load->model('news/category');

		$this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home'),
			'separator' => false
		);

		if (isset($this->request->get['article_id'])) {
			$article_id = $this->request->get['article_id'];
		} else {
			$article_id = 0;
		}
		
		$article_info = $this->model_news_article->getArticle($article_id);

		$this->data['article_info'] = $article_info;
		
		if($article_info){
			$category_info = $this->model_news_category->getCategory($article_info['category_id']);
			
			if($category_info){
				$news_type_info = $this->model_news_type->getNewsType($category_info['type_id']);
			}
		}

		if ($article_info && $category_info && $news_type_info) {
			$url = '';
			
			$this->data['article_info']['body'] = html_entity_decode($article_info['body'], ENT_QUOTES, 'UTF-8');
			
			$data_filter = array(
				'filter_category_id'	=> $article_info['category_id'],
				'filter_article_id'		=> $article_id,
				'filter_later'     	 	=> true
			);			
			$results_1 = $this->model_news_article->getArticleAround($data_filter);
			
			$this->data['news_later'] = array();			
			foreach ($results_1 as $result) {				
				$this->data['news_later'][] = array(
					'article_id' 	=> $result['article_id'],
					'title'        	=> $result['title'],
					'href' 			=> $this->url->link('news/article', 'article_id=' . $result['article_id'])
				);
			}
						
			$data_filter = array(
				'filter_category_id'	=> $article_info['category_id'],
				'filter_article_id'		=> $article_id,
				'filter_later'     	 	=> false
			);
			$results_2 = $this->model_news_article->getArticleAround($data_filter);
			
			$this->data['news_before'] = array();
			foreach ($results_2 as $result) {				
				$this->data['news_before'][] = array(
					'article_id' 	=> $result['article_id'],
					'title'        	=> $result['title'],
					'href' 			=> $this->url->link('news/article', 'article_id=' . $result['article_id'])
				);
			}
			
			$this->data['breadcrumbs'][] = array(
				'text'      => $news_type_info['name'],
				'href'      => $this->url->link('news/type', $url . '&type_id=' . $category_info['type_id']),
				'separator' => $this->language->get('text_separator')
			);

			$this->data['breadcrumbs'][] = array(
				'text'      => $category_info['name'],
				'href'      => $this->url->link('news/category', $url . '&category_id=' . $category_info['category_id']),
				'separator' => $this->language->get('text_separator')
			);

			$this->document->setTitle($article_info['title']);
			$this->document->setDescription($article_info['meta_description']);
			$this->document->setKeywords($article_info['meta_keyword']);
			$this->document->addLink($this->url->link('news/article', 'article_id=' . $this->request->get['article_id']), 'canonical');

			$this->data['heading_title'] = $article_info['title'];

			// Mapping all language variables
			$this->data = array_merge($this->data, $this->language->getData());
			
			$this->model_news_article->updateViewed($this->request->get['article_id']);

			if (file_exists(DIR_RESTAURANT_TEMPLATE . $this->config->get('config_template') . '/template/news/article.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/news/article.tpl';
			} else {
				$this->template = 'default/template/news/article.tpl';
			}

			$this->children = array(
				'common/column_left',
				'common/column_right',
				'common/content_top',
				'common/content_bottom',
				'common/footer',
				'common/header'
				);
					
				$this->response->setOutput($this->render());
		} else {
			$url = '';

			$this->data['breadcrumbs'][] = array(
        		'text'      => $this->language->get('text_error'),
				'href'      => $this->url->link('news/article', $url . '&article_id=' . $article_id),
        		'separator' => $this->language->get('text_separator')
			);

			$this->document->setTitle($this->language->get('text_error'));

			$this->data['heading_title'] = $this->language->get('text_error');

			$this->data['text_error'] = $this->language->get('text_error');

			$this->data['button_continue'] = $this->language->get('button_continue');

			$this->data['continue'] = $this->url->link('common/home');

			if (file_exists(DIR_RESTAURANT_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/error/not_found.tpl';
			} else {
				$this->template = 'default/template/error/not_found.tpl';
			}

			$this->children = array(
				'common/column_left',
				'common/column_right',
				'common/content_top',
				'common/content_bottom',
				'common/footer',
				'common/header'
				);
					
				$this->response->setOutput($this->render());
		}
	}

	public function review() {
		$this->language->load('news/article');

		$this->load->model('restaurant/review');

		$this->data['text_no_reviews'] = $this->language->get('text_no_reviews');

		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}

		$this->data['reviews'] = array();

		$review_total = $this->model_restaurant_review->getTotalReviewsByRestaurantId($this->request->get['article_id']);
			
		$results = $this->model_restaurant_review->getReviewsByRestaurantId($this->request->get['article_id'], ($page - 1) * 5, 5);

		foreach ($results as $result) {
			$this->data['reviews'][] = array(
        		'author'     => $result['author'],
				'text'       => strip_tags($result['text']),
				'rating'     => (int)$result['rating'],
        		'reviews'    => sprintf($this->language->get('text_reviews'), (int)$review_total),
        		'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added']))
			);
		}
			
		$pagination = new Pagination();
		$pagination->total = $review_total;
		$pagination->page = $page;
		$pagination->limit = 5;
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('news/article/review', 'article_id=' . $this->request->get['article_id'] . '&page={page}');
			
		$this->data['pagination'] = $pagination->render();

		if (file_exists(DIR_RESTAURANT_TEMPLATE . $this->config->get('config_template') . '/template/restaurant/review.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/restaurant/review.tpl';
		} else {
			$this->template = 'default/template/restaurant/review.tpl';
		}

		$this->response->setOutput($this->render());
	}

	public function write() {
		$this->language->load('news/article');

		$this->load->model('restaurant/review');

		$json = array();

		if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 25)) {
			$json['error'] = $this->language->get('error_name');
		}

		if ((utf8_strlen($this->request->post['text']) < 25) || (utf8_strlen($this->request->post['text']) > 1000)) {
			$json['error'] = $this->language->get('error_text');
		}

		if (!$this->request->post['rating']) {
			$json['error'] = $this->language->get('error_rating');
		}

		if (!isset($this->session->data['captcha']) || ($this->session->data['captcha'] != $this->request->post['captcha'])) {
			$json['error'] = $this->language->get('error_captcha');
		}

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && !isset($json['error'])) {
			$this->model_restaurant_review->addReview($this->request->get['article_id'], $this->request->post);

			$json['success'] = $this->language->get('text_success');
		}

		$this->response->setOutput(json_encode($json));
	}

	public function upload() {
		$this->language->load('news/article');

		$json = array();

		if (!empty($this->request->files['file']['name'])) {
			$filename = basename(html_entity_decode($this->request->files['file']['name'], ENT_QUOTES, 'UTF-8'));

			if ((strlen($filename) < 3) || (strlen($filename) > 128)) {
				$json['error'] = $this->language->get('error_filename');
			}

			$allowed = array();

			$filetypes = explode(',', $this->config->get('config_upload_allowed'));

			foreach ($filetypes as $filetype) {
				$allowed[] = trim($filetype);
			}

			if (!in_array(substr(strrchr($filename, '.'), 1), $allowed)) {
				$json['error'] = $this->language->get('error_filetype');
			}

			if ($this->request->files['file']['error'] != UPLOAD_ERR_OK) {
				$json['error'] = $this->language->get('error_upload_' . $this->request->files['file']['error']);
			}
		} else {
			$json['error'] = $this->language->get('error_upload');
		}

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && !isset($json['error'])) {
			if (is_uploaded_file($this->request->files['file']['tmp_name']) && file_exists($this->request->files['file']['tmp_name'])) {
				$file = basename($filename) . '.' . md5(rand());

				// Hide the uploaded file name sop people can not link to it directly.
				$this->load->library('encryption');

				$encryption = new Encryption($this->config->get('config_encryption'));

				$json['file'] = $encryption->encrypt($file);

				move_uploaded_file($this->request->files['file']['tmp_name'], DIR_DOWNLOAD . $file);
			}

			$json['success'] = $this->language->get('text_upload');
		}

		$this->response->setOutput(json_encode($json));
	}
}
?>